package com.itcast.flink.connectors.kafka;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * <p>Description: </p>
 *
 * @author
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 * @date
 */
public class KafkaSourceApplication {
    
    public static void main(String[] args) throws Exception {
        
        // 1. 创建运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
        // 2. 设置kafka服务连接信息
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.23.140:9092");
        properties.setProperty("group.id", "fink_group");
        
        // 3. 创建Kafka消费端
        FlinkKafkaConsumer kafkaProducer = new FlinkKafkaConsumer(
                "flink-source",                  // 目标 topic
                new SimpleStringSchema(),   // 序列化 配置
                properties);
        
        kafkaProducer.setStartFromEarliest();     // 尽可能从最早的记录开始
//        kafkaProducer.setStartFromLatest();       // 从最新的记录开始
//        kafkaProducer.setStartFromTimestamp(...); // 从指定的时间开始（毫秒）
//        kafkaProducer.setStartFromGroupOffsets(); // 默认的方法
        
        // 4. 读取Kafka数据源
        DataStreamSource<String> socketStr = env.addSource(kafkaProducer);
        
        socketStr.print().setParallelism(1);
        
        // 5. 执行任务
        env.execute("job");
    }
    
}
